package com.spn.blog.api.controller.article;


import com.spn.blog.common.dao.pojo.LoginUser;
import com.spn.blog.common.vo.Result;
import com.spn.blog.common.vo.params.CommentParam;
import com.spn.blog.service.article.CommentsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

@Api(tags = "评论模块")
@RestController
@RequestMapping("api/comments")
public class CommentsController {

    @DubboReference
    private CommentsService commentsService;

    @ApiOperation(value = "获取 - 单个文章的所有评论")
    @GetMapping("article/{id}")
    public Result comments(@PathVariable("id") Long id){
        return commentsService.comentsByArticleId(id);
    }

    @ApiOperation(value = "发表评论")
    @PostMapping("create/change")
    @PreAuthorize("hasAuthority('dev:user')")
    public Result comments(@RequestBody CommentParam commentParam){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String id = loginUser.getSysUser().getId();
        return commentsService.comment(commentParam, id);
    }
}
